

cap file close Table
pause on 

local outcomes			$outcomes

local all_outcomes      $`outcomes'

local title				`outcomes'_endline_1_Egypt_ge


* Estimate ITTs using Egypt endline 1 data 
				
	local count_out = 0 
	mat def pvalue = J(25,4,.)


	foreach outcome in `all_outcomes' {

	use  "Data/endline_1_egypt_fem.dta", clear 

		  local count_out = `count_out' + 1
			
			* Total ITT
			
				eststo total_itt: reghdfe `outcome' total_itt, cluster(vil) absorb(matchid)
				
					local c_1_`outcome' : di%12.3f _b[total_itt]
					local se_1_`outcome' : di%12.3f _se[total_itt]
					local n_1_`outcome' = e(N)
					local r2_1_`outcome' = e(R2)
					
					local p_1_`outcome' : di%12.3f ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					mat def pvalue[`count_out',1] = ttail(e(df_r),abs(_b[total_itt]/_se[total_itt]))*2
					
					sum `outcome' if total_itt ==0	
					local m_1_`outcome' : di%12.3f `r(mean)'

					local c_m1_`outcome'  =  `c_1_`outcome''/`m_1_`outcome''
					local c_m1_`outcome' 	 : di%12.3f `c_m1_`outcome'' 
		
			* Within
				
				eststo within: reghdfe `outcome' within, cluster(vil) absorb(matchid)
					
					local c_2_`outcome' : di%12.3f _b[within]
					local se_2_`outcome' : di%12.3f _se[within]
					local n_2_`outcome' = e(N)
					local r2_2_`outcome' = e(R2)
					local p_2_`outcome' : di%12.3f ttail(e(df_r),abs(_b[within]/_se[within]))*2
					
					mat def pvalue[`count_out',2] = ttail(e(df_r),abs(_b[within]/_se[within]))*2	
					
					sum `outcome' if within ==0	
					local m_2_`outcome' : di%12.3f `r(mean)'

					local c_m2_`outcome'  =  `c_2_`outcome''/`m_2_`outcome''
					local c_m2_`outcome' 	 : di%12.3f `c_m2_`outcome'' 
			
			* Between
			
				eststo between: reghdfe `outcome' between, cluster(vil) absorb(matchid)
				
					local c_3_`outcome' : di%12.3f  _b[between]
					local se_3_`outcome' : di%12.3f _se[between]
					local n_3_`outcome'  = e(N)
					local r2_3_`outcome' = e(R2)
					local p_3_`outcome' : di%12.3f ttail(e(df_r),abs(_b[between]/_se[between]))*2
					
					mat def pvalue[`count_out',3] = ttail(e(df_r),abs(_b[between]/_se[between]))*2	
					
					sum `outcome' if between ==0	
					local m_3_`outcome' : di%12.3f `r(mean)'

					local c_m3_`outcome'  =  `c_3_`outcome''/`m_3_`outcome''
					local c_m3_`outcome' 	 : di%12.3f `c_m3_`outcome'' 

					
			* Spillover  
			
				eststo spill: reghdfe `outcome' spillovers, cluster(vil) absorb(matchid)
				
					local c_4_`outcome' : di%12.3f _b[spillovers]
					local se_4_`outcome' : di%12.3f _se[spillovers]
					local n_4_`outcome' = e(N)
					local r2_4_`outcome' = e(R2)
					local p_4_`outcome' : di%12.3f ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[spillovers]/_se[spillovers]))*2
					
					sum `outcome' if spillovers ==0	
					local m_4_`outcome' : di%12.3f `r(mean)'

					local c_m4_`outcome'  =  `c_4_`outcome''/`m_4_`outcome''
					local c_m4_`outcome' 	 : di%12.3f `c_m4_`outcome'' 

			* GE  
			
				eststo infrastructure: reghdfe `outcome' infrastructure, cluster(vil) absorb(matchid)
				
					local c_9_`outcome' : di%12.3f _b[infrastructure]
					local se_9_`outcome' : di%12.3f _se[infrastructure]
					local n_9_`outcome' = e(N)
					local r2_9_`outcome' = e(R2)
					local p_9_`outcome' : di%12.3f ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					mat def pvalue[`count_out',4] = ttail(e(df_r),abs(_b[infrastructure]/_se[infrastructure]))*2
					
					sum `outcome' if infrastructure ==0	
					local m_9_`outcome' : di%12.3f `r(mean)'

					local c_m9_`outcome'  =  `c_9_`outcome''/`m_9_`outcome''
					local c_m9_`outcome' 	 : di%12.3f `c_m9_`outcome'' 
					
	}
				
				
			
* Store significance level based on p-value
	
	foreach outcome of local all_outcomes {
		
		local s_1_`outcome' ""
		local s_2_`outcome' ""
		local s_3_`outcome' ""
		local s_4_`outcome' ""
		local s_9_`outcome' ""
		
		* Total ITT 
		
			if `p_1_`outcome'' < 0.1{
				local s_1_`outcome' "*"
			}
			if `p_1_`outcome'' < 0.05{
				local s_1_`outcome' "**"
			}
			if `p_1_`outcome'' < 0.01{
				local s_1_`outcome' "***"
			}
			
		* Within
		
			if `p_2_`outcome'' < 0.1{
				local s_2_`outcome' "*"
			}
			if `p_2_`outcome'' < 0.05{
				local s_2_`outcome' "**"
			}
			if `p_2_`outcome'' < 0.01{
				local s_2_`outcome' "***"
			}
			
		* Between 
		
			if `p_3_`outcome'' < 0.1{
				local s_3_`outcome' "*"
			}
			if `p_3_`outcome'' < 0.05{
				local s_3_`outcome' "**"
			}
			if `p_3_`outcome'' < 0.01{
				local s_3_`outcome' "***"
			}
		
		* Spillover 
		
			if `p_4_`outcome'' < 0.1{
				local s_4_`outcome' "*"
			}
			if `p_4_`outcome'' < 0.05{
				local s_4_`outcome' "**"
			}
			if `p_4_`outcome'' < 0.01{
				local s_4_`outcome' "***"
			}
	
	    * GE 
		
			if `p_9_`outcome'' < 0.1{
				local s_9_`outcome' "*"
			}
			if `p_9_`outcome'' < 0.05{
				local s_9_`outcome' "**"
			}
			if `p_9_`outcome'' < 0.01{
				local s_9_`outcome' "***"
			}
	
	}
	
		
	foreach outcome of local all_outcomes {
		
		 local l_`outcome' : variable label `outcome'
		
	}
	

	
	
* Save tables





	file open Table using "Output/`title'.tex", text write replace

	file write Table  _n ///
	"\begin{tabular}{l*{17}{c}}\hline&\multicolumn{4}{c}{\textbf{General equilibrium}} \\ \cmidrule(r){2-5} & $\beta$ & {SE} & {N} & \shortstack{Control\\mean} \\ \cmidrule(r){2-5} & {(1)} & {(2)} & {(3)} & {(4)} \\ \midrule \multicolumn{1}{p{1.3cm}}{\textbf{Egypt}} & &  &    \\ \cmidrule(r){1-5}"   _n

	foreach outcome of local all_outcomes {
		
	file write Table _n ///
	"`l_`outcome'' & `c_9_`outcome''`s_9_`outcome'' & `se_9_`outcome'' & `n_9_`outcome'' & `m_9_`outcome'' \\"  _n 
	}

	file write Table _n ///
			"\hline \end{tabular}" _n

	file close Table

